﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using dhDataAcess.produceDAL;
using dhSystemBLL.produceBLL;
using dhSystemBLL.basicData;
using baseSet.importExport;
using dhDataAcess.dataAcess.produceDataSetTableAdapters;
using dhDataAcess.dataAcess;
using baseSet;
using dhDataAcess.produceLinq;
using dhDataAcess.baseBLL;
using dhSystemBLL.orderData;

namespace dhsystem.produceForm
{
    public partial class FrmorderStockReturnMasterAdd : Form
    {
        produceDataSet.orderStockReturnDetailDataTable orderStockReturnDetailTable = new produceDataSet.orderStockReturnDetailDataTable();
        orderStockReturnDetailModel orderStockReturnDetailRow = new orderStockReturnDetailModel();
        orderStockReturnBLL orderStockReturn = new orderStockReturnBLL();
        stockListBLL stockList = new stockListBLL();
        custListBLL cust = new custListBLL();
        orderStockReturnMasterModel orderStockReturnMasterRow = new orderStockReturnMasterModel();
        BindingSource b2;
        string state;
        public FrmorderStockReturnMasterAdd(BindingSource b1, string _state)
        {
            InitializeComponent();
            state = _state;
            b2 = b1;
        }
        //打开窗口时绑定数据
        private void FrmorderStockReturnMasterAdd_Load(object sender, EventArgs e)
        {

            bindingSourcecustList.DataSource = cust.getcustListAll();
            comboBoxcustNum.DataSource = bindingSourcecustList;
            comboBoxcustNum.DisplayMember = "custName";
            comboBoxcustNum.ValueMember = "custNum";
            bindingSourcestockList.DataSource = stockList.getstockListAll();
            comboBoxstockName.DataSource = bindingSourcestockList;
            comboBoxstockName.DisplayMember = "stockName";
            comboBoxstockName.ValueMember = "stockName";
            if (state == "新增")
            {
                textbillNo.Text = "R" + GlobalInfo.Date + "-" + basicSql.getOrderSerail("订单成品退库单号");
                bindingSourceorderStockReturnDetail.DataSource = orderStockReturn.getsorderStockReturnDetailAllBybillNo (textbillNo.Text);
                dGridDetail.DataSource = bindingSourceorderStockReturnDetail;

            }
            if (state == "编辑")
            {
                comboBoxcustNum.DataBindings.Add("SelectedValue", b2, "custNum");
                textworkername.DataBindings.Add("text", b2, "workername");
                textorderNo.DataBindings.Add("text", b2, "orderNo");
                textbillNo.DataBindings.Add("text", b2, "billNo");
                dateTimeoutDate.DataBindings.Add("value", b2, "outDate");
                comboBoxstockName.DataBindings.Add("text", b2, "stockName");
                textmark.DataBindings.Add("text", b2, "mark");
                bindingSourceorderStockReturnDetail.DataSource = orderStockReturn.getsorderStockReturnDetailAllBybillNo (textbillNo.Text);
                dGridDetail.DataSource = bindingSourceorderStockReturnDetail;
            }
            DcolumnSet.colSet(moduleInfo.orderStockReturnDetail, dGridDetail);
            dGridDetail.AllowUserToAddRows = false;
        }
        int i = 0;
        //保存按钮，插入或修改数据
        private void buttonOK_Click(object sender, EventArgs e)
        {
            if (textbillNo.Text == "")
            {
                MessageBox.Show("请输入库单号");
            }
            else
            {
                try
                {
                    DialogResult result = MessageBox.Show("是否保存成品退库库表信息！", "提示信息", MessageBoxButtons.YesNo);
                    if (result == DialogResult.Yes)
                    {

                        if (i == 0 && state == "新增")
                        {
                            state = "新增";
                        }
                        else
                        {
                            state = "编辑";
                        }
                        i++;
                        dhDataAcess.dataAcess.produceDataSet.orderStockReturnDetailDataTable ptable = (dhDataAcess.dataAcess.produceDataSet.orderStockReturnDetailDataTable)bindingSourceorderStockReturnDetail.DataSource;
                        orderStockReturnMasterRow.billNo = textbillNo.Text;
                        orderStockReturnMasterRow.workername = textworkername.Text;
                        orderStockReturnMasterRow.outDate = dateTimeoutDate.Value;
                        orderStockReturnMasterRow.orderNo = textorderNo.Text;
                        orderStockReturnMasterRow.billStatus = "";
                        orderStockReturnMasterRow.stockName = comboBoxstockName.SelectedValue.ToString();
                        orderStockReturnMasterRow.custNum = comboBoxcustNum.SelectedValue.ToString();
                        orderStockReturnMasterRow.updateDate = DateTime.Now;
                        orderStockReturnMasterRow.updateUser = GlobalInfo.UserName;
                        orderStockReturnMasterRow.mark = textmark.Text;
                        orderStockReturn.orderStockReturnInsert (orderStockReturnMasterRow, ptable, state, textbillNo.Text, GlobalInfo.Date);
                        ptable.AcceptChanges();
                        bindingSourceorderStockReturnMaster.DataSource = orderStockReturn.getorderStockOutMasterAll();
                        MessageBox.Show("保存成功");
                    }
                }
                catch (FormatException ex)
                {
                    MessageBox.Show(ex.Message.ToString());
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message.ToString());
                }
            }

        }
        //取消按钮
        private void buttonCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }
        //删除子件
        private void buttDel_Click(object sender, EventArgs e)
        {

            try
            {
                string id = dGridDetail.Rows[dGridDetail.CurrentRow.Index].Cells[0].Value.ToString();
                DialogResult result = MessageBox.Show("是否删除：" + id + "！", "提示信息", MessageBoxButtons.YesNo);
                if (result == DialogResult.Yes)
                {
                    dGridDetail.Rows.Remove(dGridDetail.CurrentRow);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message.ToString());
            }
        }
        //编辑子件
        private void buttedit_Click(object sender, EventArgs e)
        {
            FrmorderStockReturnDetailAdd orderStockReturnDetail = new FrmorderStockReturnDetailAdd(bindingSourceorderStockReturnDetail, "编辑", textbillNo.Text);
            orderStockReturnDetail.ShowDialog();
        }
        //添加子件
        private void buttAdd_Click(object sender, EventArgs e)
        {
            FrmorderStockReturnDetailAdd orderStockReturnDetail = new FrmorderStockReturnDetailAdd(bindingSourceorderStockReturnDetail, "新增", textbillNo.Text);
            orderStockReturnDetail.ShowDialog();
        }
    }
}
